package Lecture1;

import java.util.Iterator;

/**
 * Created by Win7Pronly on 23.03.14.
 */
public class Bag <Item> implements Iterable <Item> {
    private Entry entries;
    private int size;

    public Bag(){
        entries = new Entry();
        size = 0;
    }

    @Override
    public Iterator<Item> iterator() {
        return new BagIterator();
    }






    public void add (Item x){
        Entry old = entries;
        entries.item = x;
        entries.next = old;
        size++;
    }

    int size(){
        return size;
    }

    private class Entry{
        Item item;
        Entry next;
    }

    private class BagIterator implements Iterator <Item>{
        Entry current;

        BagIterator(){
            current = entries;
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item output = current.item;
            current = current.next;
            return  output;
        }

        @Override
        public void remove() {

        }
    }
}
